/*
 * This file is part of Avenge <https://avenge.cn/>,
 * Copyright (C) 2006-present eyeo GmbH
 *
 * Avenge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Avenge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Avenge.  If not, see <http://www.gnu.org/licenses/>.
 */

"use strict";

const request = require("request");

exports.downloadJSON = function(url)
{
  return new Promise((resolve, reject) =>
  {
    request(url, (err, response, body) =>
    {
      if (err)
        reject(err);
      else if (response.statusCode != 200)
        reject(new Error("Request failed with status code " +
                         response.statusCode));
      else
      {
        try
        {
          resolve(JSON.parse(body));
        }
        catch (e)
        {
          reject(e);
        }
      }
    });
  });
};
